package com.jwthhealth_pub.old.heartscreen.presenter;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.heartscreen.module.HeartScreenModule;
import com.jwthhealth_pub.old.heartscreen.view.HeartScreenActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/1/17.
 */

public class IHeartScreenPresenterCompl implements IHeartScreenPresenter {

    private HeartScreenActivity activity;

    public IHeartScreenPresenterCompl(HeartScreenActivity heartScreenActivity) {
        this.activity = heartScreenActivity;
    }

    @Override
    public void commit(String weight, String heart, String age, String type, String sex, String time) {

        LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
        Call<HeartScreenModule> heartScreenModuleCall;
        if (userinfoModel != null) {
            heartScreenModuleCall = ApiHelper.RequestHeartScreen(weight, heart, age, userinfoModel.getId(), type, sex, time, userinfoModel.getAndroidtoken());
        } else {
            heartScreenModuleCall = ApiHelper.RequestHeartScreen(weight, heart, age, null, type, sex, time, null);
        }
            heartScreenModuleCall.enqueue(new Callback<HeartScreenModule>() {
                @Override
                public void onResponse(Call<HeartScreenModule> call, Response<HeartScreenModule> response) {
                    final HeartScreenModule module = response.body();
                    if (module.getCode().equals("0")) {
                        Message message = new Message();
                        message.what = 0;
                        Bundle bundle = new Bundle();
                        bundle.putSerializable(Constant.HEARTSCREENINGDATA, module);
                        message.setData(bundle);
                        activity.mHandler.sendMessage(message);
                    } else {
                        activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(activity, module.getMsg(), Toast.LENGTH_SHORT).show();
                                activity.progressbar();
                            }
                        });
                    }
                }

                @Override
                public void onFailure(Call<HeartScreenModule> call, Throwable t) {
                    Log.d("IHeartScreenPresenterCo", "error");
                }
            });
//        else {
//            activity.runOnUiThread(new Runnable() {
//                @Override
//                public void run() {
//                    Toast.makeText(activity, "请登录", Toast.LENGTH_SHORT).show();
//                    activity.progressbar();
//                }
//            });
//        }
    }
}
